package com.gangwangchina.android.contactlib.model;

import java.util.HashMap;
import java.util.Map;

import com.gangwangchina.android.contactlib.ContactsInterface;



import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;

public class PostalAddress extends DataElement {
	
	public static final String FORMATTED_ADDRESS = StructuredPostal.FORMATTED_ADDRESS;
	public static final String TYPE = StructuredPostal.TYPE;
	public static final String LABEL = StructuredPostal.LABEL;
	public static final String STREET = StructuredPostal.STREET;
	public static final String POBOX = StructuredPostal.POBOX;
	public static final String NEIGHBORHOOD = "data[6]"; // StructuredPostal.NEIGHBORHOOD;
	public static final String CITY = StructuredPostal.CITY;
	public static final String REGION = StructuredPostal.REGION;
	public static final String POSTCODE = StructuredPostal.POSTCODE;
	public static final String COUNTRY = StructuredPostal.COUNTRY;
	public static final String CONTENT_ITEM_TYPE = StructuredPostal.CONTENT_ITEM_TYPE;

	public int type;
	public String label, street, pobox, neighborhood, city, region, postcode,
			country;

	/**
	 * Creates a new Postal Address that is not (yet) present in the database
	 * 
	 * @param type
	 * @param label
	 * @param street
	 * @param pobox
	 * @param neighborhood
	 * @param city
	 * @param region
	 * @param postcode
	 * @param country
	 * @return
	 */
	public static PostalAddress newPostalAddress(int type, String label,
			String street, String pobox, String neighborhood, String city,
			String region, String postcode, String country) {
		return new PostalAddress(type, label, street, pobox, neighborhood,
				city, region, postcode, country);
	}

	/**
	 * Creates an object representation of a Telephone Number that is present in
	 * the database
	 * 
	 * @param pcId
	 * @param type
	 * @param label
	 * @param street
	 * @param pobox
	 * @param neighborhood
	 * @param city
	 * @param region
	 * @param postcode
	 * @param country
	 * @return
	 */
	public static PostalAddress recoverPostalAddress(long pcId, int type,
			String label, String street, String pobox, String neighborhood,
			String city, String region, String postcode, String country) {
		return new PostalAddress(pcId, type, label, street, pobox,
				neighborhood, city, region, postcode, country);
	}

	private PostalAddress(long pcId) {
		super(pcId, CONTENT_ITEM_TYPE);
	}

	private PostalAddress(long pcId, int type, String label, String street,
			String pobox, String neighborhood, String city, String region,
			String postcode, String country) {
		this(pcId);
		this.type = type;
		this.label = label;
		this.street = street;
		this.pobox = pobox;
		this.neighborhood = neighborhood;
		this.city = city;
		this.region = region;
		this.postcode = postcode;
		this.country = country;
	}

	private PostalAddress(int type, String label, String street, String pobox,
			String neighborhood, String city, String region, String postcode,
			String country) {
		this(ContactsInterface.NO_ID, type, label, street, pobox, neighborhood,
				city, region, postcode, country);
	}

	public String formattedAddress() {
		StringBuilder sb = new StringBuilder();
		if (street != null)
			sb.append(street).append('\n');
		if (pobox != null)
			sb.append(pobox).append('\n');
		if (neighborhood != null)
			sb.append(neighborhood).append('\n');
		if (city != null) {
			sb.append(city);
			if (region != null)
				sb.append(", ").append(region);
			if (postcode != null)
				sb.append(' ').append(postcode);
		} else {
			if (region != null) {
				sb.append(region);
				if (postcode != null)
					sb.append(' ').append(postcode);
			} else {
				if (postcode != null)
					sb.append(postcode);
			}
		}
		return sb.toString();
	}

	@Override
	public Map<String, Object> getDataColumns() {
		Map<String, Object> datas = new HashMap<String, Object>();
		datas.put(FORMATTED_ADDRESS, formattedAddress());
		datas.put(STREET, street);
		datas.put(POBOX, pobox);
		datas.put(NEIGHBORHOOD, neighborhood);
		datas.put(CITY, city);
		datas.put(REGION, region);
		datas.put(POSTCODE, postcode);
		datas.put(COUNTRY, country);
		// datas.put(CONTENT_ITEM_TYPE, formattedAddress());
		datas.put(LABEL, label);
		datas.put(TYPE, String.valueOf(type));
		return datas;
	}
	
	@Override
	public boolean setProperty(String key, Object value) {
		if (key.equals(TYPE))
			type = new Integer((Integer) value);
		else if (key.equals(LABEL))
			label = (String) value;
		else if (key.equals(STREET))
			street = (String) value;
		else if (key.equals(POBOX))
			pobox = (String) value;
		else if (key.equals(NEIGHBORHOOD))
			neighborhood = (String) value;
		else if (key.equals(CITY))
			city = (String) value;
		else if (key.equals(REGION))
			region = (String) value;
		else if (key.equals(POSTCODE))
			postcode = (String) value;
		else if (key.equals(COUNTRY))
			country = (String) value;
		else
			return super.setProperty(key, value);
		return true;
	}

	@Override
	public Object getProperty(String key) {
		if (key.equals(TYPE))
			return String.valueOf(type);
		else if (key.equals(LABEL))
			return label;
		else if (key.equals(STREET))
			return street;
		else if (key.equals(POBOX))
			return pobox;
		else if (key.equals(NEIGHBORHOOD))
			return neighborhood;
		else if (key.equals(CITY))
			return city;
		else if (key.equals(REGION))
			return region;
		else if (key.equals(POSTCODE))
			return postcode;
		else if (key.equals(COUNTRY))
			return country;
		else
			return super.getProperty(key);
	}

	@Override
	public String toString() {
		return formattedAddress();
	}

}
